package xcity

import (
	"gitee.com/xiaoyutab/xgotool/optional/xcache"
	"gitee.com/xiaoyutab/xgotool/xerror"
)

// Adcode换取城市详情信息
//
//	adcode	省市区的Adcode值【六位数字】
func Ad2info(adcode uint) (*City, error) {
	cache_key := xcache.Key("xcity.ad2info", adcode)
	cit := City{}
	if xcache.Exists(cache_key) {
		if err := xcache.GetStruct(cache_key, &cit); err == nil {
			return &cit, nil
		}
	}
	if _default.DB == nil {
		return nil, xerror.New("数据库未连接")
	}
	cit.Adcode = adcode
	// 从数据库中进行读取
	err := _default.DB.Table(_default.CityName).Find(&cit).Error
	if err != nil {
		return nil, err
	}
	xcache.SetStruct(cache_key, cit)
	return &cit, nil
}
